﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Use this to stop the page going back to the previous state of the edit, i.e. before the button was clicked
        If IsPostBack = False Then

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM cakers WHERE userUName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()


                If Not IsDBNull(dr("userLName")) Then tb_userLName.Text = dr("userLName")
                If Not IsDBNull(dr("userDesc")) Then tb_userDesc.Text = dr("userDesc")
                If Not IsDBNull(dr("userUrl")) Then tb_userUrl.Text = dr("userUrl")
                If Not IsDBNull(dr("userTwitter")) Then tb_userTwitter.Text = dr("userTwitter")
            End If
        End If




    End Sub

    Protected Sub btn_saveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_saveProfile.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        'Check to see if the picture file has been added for upload
        If fu_profilePicture.HasFile Then

            'Stop it creating a load of zeroes a generate an actual GUID
            Dim myGUID = Guid.NewGuid
            'Turn this GUID into a string and add the jpeg file extension to make a unique file name (but what if it was a .gif or .tiff etc?)
            newFileName = myGUID.ToString() + ".jpg"

            'Find the pictures folder and save the image there using its randomnly generated filename
            Dim picLocationOnServerHardDisk = Request.MapPath("img/profile") & "/" & newFileName
            fu_profilePicture.SaveAs(picLocationOnServerHardDisk)

        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        SqlString = "UPDATE cakers SET userFName=@f1, userLName=@f2, userLoc=@f3,  userDesc=@f4,  userUrl=@f5, userTwitter=@f6, userImg=@f7, userAge=@f8  WHERE userUName=@f9"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_userFName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_userLName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_userLoc.SelectedItem.ToString())
        cmd.Parameters.AddWithValue("@f4", tb_userDesc.Text)
        cmd.Parameters.AddWithValue("@f5", tb_userUrl.Text)
        cmd.Parameters.AddWithValue("@f6", tb_userTwitter.Text)
        cmd.Parameters.AddWithValue("@f7", "img/profile/" & newFileName)
        cmd.Parameters.AddWithValue("@f8", tb_userAge.Text)
        cmd.Parameters.AddWithValue("@f9", Request.QueryString("userUName"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("cakerProfile.aspx?userUName=" & User.Identity.Name)
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub tb_userUrl_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tb_userUrl.TextChanged

    End Sub
End Class